let jia, jian, one, totalPrices, zongShu, checkAll, inputAll, checkShop, checkSelf;

onload = () => {
    jia = document.querySelectorAll(".jia");//加数量
    jian = document.querySelectorAll(".jian");//减数量
    one = document.querySelectorAll(".one");//数量输入框
    totalPrices = document.getElementsByClassName("totalPrices");//行总价
    zongShu = document.querySelectorAll("#zongShu")[0];//全部总价
    checkSelf = document.querySelectorAll(".checkSelf");//获得选自己按钮
    additionw();//加减input
    del();//单行删除and批量删除
    closePage();//关闭购物车
    checkedBox();
};

//求购物车总价
function getPrice() {
    let arr = [];
    //如果选中则把总价放入数组求和
    for (let i = 0; i < checkSelf.length; i++) {
        if (checkSelf[i].checked === true) {
            //em=checkSelf[i].nextElementSibling.nextElementSibling.nextElementSibling.nextElementSibling.nextElementSibling.nextElementSibling.lastElementChild.textContent;
            for (let p = 0; p < totalPrices.length; p++) {
                if (p === i) {
                    arr.push(totalPrices[p])
                }
            }
        }
    }
    //console.log(arr)
    let m = 0;
    for (let x = 0; x < arr.length; x++) {
        m += Number(arr[x].textContent);
        // m=m.toFixed(2)
    }
    zongShu.innerHTML = m.toFixed(2)
}

//删除商品
function del() {
    let deleteThis = document.querySelectorAll(".deleteThis");
    for (let i = 0; i < deleteThis.length; i++) {
        //删除商品
        deleteThis[i].onclick = () => {
            for (let l = 0; l < checkSelf.length; l++) {
                if (l === i) {
                    //选中才能删
                    if (checkSelf[l].checked === true) {
                        let divBj = document.createElement("div");
                        document.body.appendChild(divBj);//背景
                        divBj.className = "divBj";
                        let sureDelete = document.querySelector(".sureDelete");
                        sureDelete.style.display = "block";//弹框
                        let sure = sureDelete.lastElementChild.previousElementSibling;
                        let cancel = sureDelete.lastElementChild;
                        //确认删除
                        sure.onclick = () => {
                            sureDelete.style.display = "none";
                            divBj.style.display = "none";
                            let shopping = deleteThis[i].parentNode.parentNode;
                            shopping.parentNode.removeChild(shopping);
                            //如果没有商品，显示购物车为空
                            let jia=document.querySelectorAll(".jia");
                            if (jia.length===0){
                                let main = document.querySelector(".main");
                                let footer = document.querySelector(".footer");
                                main.innerHTML = "您的购物车为空";
                                footer.innerHTML = ""
                            }
                        };
                        //取消删除
                        cancel.onclick = () => {
                            sureDelete.style.display = "none";
                            divBj.style.display = "none";
                        }
                    } else {
                        alert("请选择您要删除的商品")
                    }
                }
            }
        }
    }
//批量删除
    let deleteAll = document.querySelector(".deleteAll");
    deleteAll.onclick = () => {
        //del()
          let status=false;//有选中时让标记改变，删除；没有选中就不删
            for (let i = 0; i < checkSelf.length; i++) {
                if (checkSelf[i].checked ) {
                   //alert("能删")
                   status = true;
                    break;
                }
            }
            if (status===false){
                alert("请选择您要删除的商品")
            }else {
                let divBj = document.createElement("div");
                document.body.appendChild(divBj);//背景
                divBj.className = "divBj";
                let sureDelete = document.querySelector(".sureDelete");
                sureDelete.style.display = "block";//弹框
                let sure = sureDelete.lastElementChild.previousElementSibling;
                let cancel = sureDelete.lastElementChild;
                //确认删除
                sure.onclick = () => {
                    sureDelete.style.display = "none";
                    divBj.style.display = "none";
                    for (let i = 0; i < checkSelf.length; i++) {
                        if (checkSelf[i].checked ) {
                            let shopping = deleteThis[i].parentNode.parentNode;
                            shopping.parentNode.removeChild(shopping);
                            //如果没有商品，显示购物车为空
                            let jia=document.querySelectorAll(".jia");
                            if (jia.length===0){
                                let main = document.querySelector(".main");
                                let footer = document.querySelector(".footer");
                                main.innerHTML = "您的购物车为空";
                                footer.innerHTML = ""
                            }
                            }
                        }
                };
                //取消删除
                cancel.onclick = () => {
                    sureDelete.style.display = "none";
                    divBj.style.display = "none";
                }
            }
    }
}
//关闭购物车
function closePage() {
    let closePage = document.querySelector(".closePage");
    closePage.onclick = () => {
        window.close()
    }
}
//加减输入框
function additionw() {
//遍历+，对input框赋值
    for (let i = 0; i < jia.length; i++) {
        jia[i].onclick = () => {
            let input = jia[i].previousElementSibling;
            input.value++;
            if (input.value > 10) {
                input.value = 10
            }
            let p = jia[i].parentNode.previousElementSibling.lastChild.textContent;
            jia[i].parentNode.nextElementSibling.lastChild.textContent = (input.value * p).toFixed(2);
            getPrice()
        }
    }//+
//遍历-，对input框赋值
    for (let i = 0; i < jian.length; i++) {
        jian[i].onclick = () => {
            let input = jian[i].nextElementSibling;
            input.value--;
            if (input.value < 0) {
                input.value = 0
            }
            let p = jian[i].parentNode.previousElementSibling.lastChild.textContent;
            jian[i].parentNode.nextElementSibling.lastChild.textContent = (input.value * p).toFixed(2);
            getPrice()
        }
    }//-
    //数量框失去焦点时触发操作
    let input = document.querySelectorAll(".one");
    for (let i = 0; i < input.length; i++) {
        input[i].onblur = () => {
            let num = input[i].value;
            var reg = /^\d+$/;
            if (!reg.test(num)) {
                alert("请输入数值！");
                num = 1;
            }
            if (num > 10) {
                num = 10;
            }
            if (num <= 0) {
                num = 1;
            }
            input[i].value = num;
            let p = input[i].parentNode.previousElementSibling.lastChild.textContent;
            input[i].parentNode.nextElementSibling.lastChild.textContent = (input[i].value * p).toFixed(2);
            getPrice()
        };
    }
}

function checkedBox() {
    //点击checkedBox效果
    checkAll = document.querySelectorAll(".checkAll");//获得全选按钮
    inputAll = document.getElementsByClassName("inp");//获得全部单选按钮
    for (let i = 0; i < checkAll.length; i++) {
        //单击全选
        checkAll[i].onclick = () => {
            let temp = checkAll[i].checked;
            for (let j = 0; j < inputAll.length; j++) {
                inputAll[j].checked = temp;
            }
            getPrice()//调用计算总价函数
        };
    }

    checkShop = document.querySelectorAll(".checkShop");//获得选店铺按钮
    for (let i = 0; i < checkShop.length; i++) {
        checkShop[i].onclick = () => {
            let temp = checkShop[i].checked;
            for (let j = 0; j < checkSelf.length; j++) {
                checkSelf[j].checked = temp;
            }
            getPrice()//调用计算总价函数
        };
    }

    checkSelf = document.querySelectorAll(".checkSelf");//获得选自己按钮
    for (let i = 0; i < checkSelf.length; i++) {
        checkSelf[i].onclick = () => {
            let flag = true;
            // getPrice()
            //获得选自己的状态
            for (let j = 0; j < checkSelf.length; j++) {
                if (checkSelf[j].checked === false) {
                    flag = false;
                    break;
                }
            }
            //全选的状态来自于选自己的状态
            for (let k = 0; k < checkAll.length; k++) {
                checkAll[k].checked = flag;
            }
            getPrice()//调用计算总价函数
        };
    }
}



